package com.action;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.*;
import com.service.AreaService;
import com.service.CountryService;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/guojia")
public class CountryAction {

    @Autowired
    private CountryService countryService;
    @Autowired
    private AreaService areaService;
    @Value("${upload.dir}")
    private String upload;

    @Value("${upload.tmp.dir}")
    private String tmp;
    /**
     * 分页查询国家
     * @param name  关键字
     * @param page  页面
     * @param limit  条数
     * @return
     */
    @RequestMapping("/selectCountrys")
    public ResponseBean selectCountrys(String name, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit){
        IPage iPage = countryService.selectCountrys(page,limit,name);
        return new ResponseBean(0,iPage.getTotal(),"查询成功",iPage.getRecords());
    }
    /**
     * 分页查询地区
     * @param name  关键字
     * @param country_id 国家ID
     * @param page  页面
     * @param limit  条数
     * @return
     */
    @RequestMapping("/selectAreas")
    public ResponseBean selectAreas(String name,String country_id, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit){
        IPage iPage = areaService.selectAreas(page,limit,name,country_id);
        return new ResponseBean(0,iPage.getTotal(),"查询成功",iPage.getRecords());
    }
    /**
     * 新增国家
     * @param   country  国家
     * @return
     */
    @RequestMapping("/addCountry")
    public ResponseBean addCountry(@RequestBody Country country){

        if(StringUtil.isNull(country.getCountry_zh_name())){
            return new ResponseBean(1,1,"国家中文名称不能为空","");
        }
        if(StringUtil.isNull(country.getCountry_en_name())){
            return new ResponseBean(1,1,"国家英文名称不能为空","");
        }

        if(countryService.save(country)){
            if (StringUtil.isNotNull(country.getImage())) {
                FileUtil.copy(tmp + country.getImage(), upload, true) ;
                //tmp复制图片到upload
//                String srcPathStr = tmp + notice.getPicture(); //源文件地址
//                String desPathStr = upload; //目标文件地址
//                CopyFile.copy(srcPathStr, desPathStr);
            }
            return new ResponseBean(0,1,"新增成功","");
        }
        return new ResponseBean(1,1,"操作失败","");
    }

    /**
     * 修改国家
     * @param country  国家
     * @return
     */
    @RequestMapping("/updateCountry")
    public ResponseBean updateCountry(@RequestBody Country country) {
        Country queryNotice = countryService.getById(country.getId());
        if(StringUtil.isNull(country.getCountry_zh_name())){
            return new ResponseBean(1,1,"国家中文名称不能为空","");
        }
        if(StringUtil.isNull(country.getCountry_en_name())){
            return new ResponseBean(1,1,"国家英文名称不能为空","");
        }
        if (countryService.updateById(country)) {
            if(StringUtil.isNotNull(country.getImage()) && country.getImage().equals(queryNotice.getImage())==false){
                FileUtil.copy(tmp + country.getImage(), upload, true) ;
                //tmp复制图片到upload
//                String srcPathStr = tmp + notice.getPicture(); //源文件地址
//                String desPathStr = upload; //目标文件地址
//                CopyFile.copy(srcPathStr, desPathStr);
            }
            return new ResponseBean(0, 1, "修改成功","");
        }
        return new ResponseBean(1, 1, "操作失败","");
    }
    /**
     * 新增区域
     * @param   area  区域
     * @return
     */
    @RequestMapping("/addArea")
    public ResponseBean addArea(@RequestBody Area area){

        if(StringUtil.isNull(area.getArea_zh_name())){
            return new ResponseBean(1,1,"区域中文名称不能为空","");
        }
        if(StringUtil.isNull(area.getArea_en_name())){
            return new ResponseBean(1,1,"区域英文名称不能为空","");
        }
        if(StringUtil.isNull(area.getCountry_id())){
            return new ResponseBean(1,1,"所属国家不能为空","");
        }
        if(areaService.save(area)){
            return new ResponseBean(0,1,"新增成功","");
        }
        return new ResponseBean(1,1,"操作失败","");
    }

    /**
     * 修改区域
     * @param area  区域
     * @return
     */
    @RequestMapping("/updateArea")
    public ResponseBean updateArea(@RequestBody Area area) {

        if(StringUtil.isNull(area.getArea_zh_name())){
            return new ResponseBean(1,1,"区域中文名称不能为空","");
        }
        if(StringUtil.isNull(area.getArea_en_name())){
            return new ResponseBean(1,1,"区域英文名称不能为空","");
        }
        if(StringUtil.isNull(area.getCountry_id())){
            return new ResponseBean(1,1,"所属国家不能为空","");
        }
        if (areaService.updateById(area)) {
            return new ResponseBean(0, 1, "修改成功","");
        }
        return new ResponseBean(1, 1, "操作失败","");
    }
    /**
     * 根据国家ID查区域
     * @param country_id  国家ID
     * @return
     */
    @RequestMapping("/selectAreasBycountryID")
    public ResponseBean selectAreasBycountryID(String country_id){
        QueryWrapper<Area> queryWrapper = new QueryWrapper();
        queryWrapper.eq("country_id",country_id);
        List<Area> list = areaService.list(queryWrapper);
        return new ResponseBean(0,list.size(),"查询成功",list);
    }
}
